package com.ews.onlineexchange.rest;
import java.util.Date;
import java.util.List;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.ews.onlineexchange.app.AppService;
import com.ews.onlineexchange.app.entity.SimpleCert;
import com.ews.onlineexchange.app.entity.TempQualificationCert;
import com.ews.onlineexchange.model.ProfessionUser;
import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.RejectRecord;
import com.ews.onlineexchange.service.CertHome;
import com.ews.onlineexchange.service.MessgaePushService;
import com.ews.onlineexchange.service.RejectRecordHome;
import com.ews.onlineexchange.service.UnauditCertList;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.service.qualifier.MessagePushWsEvent;
import com.ews.onlineexchange.util.RestParamUtil;

import net.sf.json.JSONObject;


@Path(value = "/profession")
@Transactional
public class ProfessionRest {
	@Inject
	EntityManager entityManager;
	@Inject
	AppService appService;

	@Inject
	UnauditCertList unauditCertList;
	
	@GET
	@Path("/getUnauditedCertList/{pageNumber}/{pageSize}/{supplierName}")
	@Produces("application/json")
	@Context
	public JSONObject getUnappledBulletinList(@PathParam("pageNumber") int pageNumber,@PathParam("pageSize")int pageSize,
			@PathParam("supplierName") String supplierName) throws Exception{        
        unauditCertList.refresh();
        unauditCertList.setFirstResult((pageNumber-1) * pageSize);
        unauditCertList.setMaxResults(pageSize);
        supplierName = RestParamUtil.getParamValue(supplierName);
        unauditCertList.setSupplierName_app(supplierName);
        Long totalcount = unauditCertList.getResultCount();
        List<QualificationCert> certList = unauditCertList.getResultList();
        List<SimpleCert> sCertList = appService.getSimpleCert(certList);
        JSONObject jsonnObject = new JSONObject();
        jsonnObject.put("totalcount", totalcount);
        jsonnObject.put("certArr", sCertList);
		return jsonnObject;
	}
	
	@Inject
	CertHome certHome;
	
	@GET
	@Path("/getCertById/{id}")
	@Produces("application/json")
	@Context
	public TempQualificationCert getServiceSupplierCertsById(@PathParam("id") String id)  {
		certHome.setId(id);
		QualificationCert cert = certHome.getInstance();
		TempQualificationCert tempCert = appService.getTempCert(cert);
		return tempCert;
	}
	
	@POST
	@Path("/auditCert/{userId}/{certId}")
	@Produces("text/html; charset=UTF-8")
	@Context
	public String auditCert(@PathParam("userId") String userId,@PathParam("certId") String certId) throws Exception {
		ProfessionUser professionUser = entityManager.find(ProfessionUser.class, userId);
		QualificationCert cert = entityManager.find(QualificationCert.class, certId);
		cert.setProfessionUser(professionUser);
		cert.setAuditResult(true);
		cert.setValid(true);
		cert.setAuditTime(new Date());
        try {
        	entityManager.merge(cert);
        	//中介资质审核成功消息推送
			String pushMessage = messgaePushService.addCertAuditSuccessSupplier(cert);
			messagePushWsEvent.fire(pushMessage);
        	return "审核成功！";
		} catch (Exception e) {
			return "审核出错，请稍后重试！";
		}
	}
	
	@Inject
	RejectRecordHome rejectRecordHome;
	
	@Inject
	@MessagePushWsEvent
	Event<String> messagePushWsEvent;
	@Inject
	MessgaePushService messgaePushService;
	
	@POST
	@Path("/rejectCert/{userId}/{certId}/{reason}")
	@Produces("text/html; charset=UTF-8")
	@Context
	public String rejectCert(@PathParam("userId") String userId,@PathParam("certId") String certId,@PathParam("reason") String reason) throws Exception {
		ProfessionUser professionUser = entityManager.find(ProfessionUser.class, userId);
		QualificationCert cert = entityManager.find(QualificationCert.class, certId);
		RejectRecord record = new RejectRecord();
		cert.setProfessionUser(professionUser);
		record.setReason(reason);
		record.setRejectTime(new Date());
		cert.setCurrRejectRecord(record);
		cert.getHistoryRejectRecords().add(record);
		cert.setProfessionUser(professionUser);
		cert.setAuditResult(false);
		cert.setValid(false);
		cert.setAuditTime(new Date());
		try {
			entityManager.merge(cert);
			//中介资质驳回消息推送
			String pushMessage = messgaePushService.addCertRejectSupplier(cert);
			messagePushWsEvent.fire(pushMessage);
        	return "驳回成功！";
		} catch (Exception e) {
				return "驳回出错，请稍后重试！";
		}
	}
}
